package adventure.commands;

import adventure.*;
import player.*;

/**
 *
 * @author Michael Hanns
 *
 */
public class Log extends AbstractCommand {

    public Log(Adventure a) {
        super(a, new String[]{"LOG", "READLOG", "READCOMPLETE", "READENTRY"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (args.length == 1) {
            if ("LOG".startsWith(args[0].toUpperCase())) {
                return readLog(p, 0);
            } else if ("READLOG".startsWith(args[0].toUpperCase())) {
                return readLog(p, 1);
            } else if ("READCOMPLETE".startsWith(args[0].toUpperCase())) {
                return readCompleteLog(p, 1);
            } else {
                return "Read which entry?";
            }
        } else if ("LOG".startsWith(args[0].toUpperCase())) {
            return readLog(p, Util.consolidateArguments(args));
        } else if ("READLOG".startsWith(args[0].toUpperCase())) {
            return readLog(p, Util.consolidateArguments(args));
        } else if ("READCOMPLETE".startsWith(args[0].toUpperCase())) {
            return readCompleteLog(p, Util.consolidateArguments(args));
        } else if ("READENTRY".startsWith(args[0].toUpperCase())) {
            return readLogEntry(p, Util.consolidateArguments(args));
        } else {
            return "Command not recognised.";
        }
    }

    private String readLog(Player player, String pageText) {
        int page = 0;

        try {
            page = Integer.parseInt(pageText);
        } catch (NumberFormatException e) {
            return "That is not a valid page number!";
        }
        return player.viewLog(page);
    }

    private String readLog(Player player, int page) {
        return player.viewLog(page);
    }

    private String readCompleteLog(Player player, String pageText) {
        int page = 0;

        try {
            page = Integer.parseInt(pageText);
        } catch (NumberFormatException e) {
            return "That is not a valid page number!";
        }

        return player.viewCompleteLog(page);
    }

    private String readCompleteLog(Player player, int page) {
        return player.viewCompleteLog(page);
    }

    private String readLogEntry(Player player, String idText) {
        int id = 0;

        try {
            id = Integer.parseInt(idText);
        } catch (NumberFormatException e) {
            return "That is not a valid entry id number!";
        }

        return player.viewLogEntry(id);
    }
}
